<template>
  <el-container>
    <el-header>
      <img class="mlogo" src="../assets/logo.png" alt="">
    </el-header>
    <el-main>
      <el-form :model="ruleForm" :rules="rules" ref="ruleFormRef" label-width="100px" class="demo-ruleForm">
        <el-form-item label="用户名" prop="username">
          <el-input v-model="ruleForm.username"></el-input>
        </el-form-item>
        <el-form-item label="密码" prop="password">
          <el-input type="password" v-model="ruleForm.password"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="submitForm">登陆</el-button>
          <el-button type="warning" @click="resetForm">重置</el-button>
        </el-form-item>
      </el-form>
    </el-main>
  </el-container>
</template>

<script>
export default {
  data() {
    return {
      ruleForm: {
        username: 'Anzia',
        password: '123456'
      },
      rules: {
        username: [
          {
            required: true,
            message: '请输入用户名',
            trigger: 'blur',
            min: 3,
            max: 15
          }
        ],
        password: [
          {
            required: true,
            message: '请输入密码',
            trigger: 'blur',
            min: 6,
            max: 15
          }
        ]
      }
    }
  },
  created() {
  },
  methods: {
    submitForm() {
      this.$refs.ruleFormRef.validate(valid => {
        if (!valid) return
        const _this = this
        this.$axios.post('/login', this.ruleForm).then(res => {
          const jwt = res.headers['authorization']
          const userInfo = res.data.data
          console.log(userInfo)

          // 把数据共享出去
          _this.$store.commit("SET_TOKEN", jwt)
          _this.$store.commit("SET_USERINFO", userInfo)

          // 获取
          console.log(_this.$store.getters.getUser)

          this.$router.push("/")
          this.$message.success('登陆成功！')
        })
      })
    },
    resetForm() {
      this.$refs.ruleFormRef.resetFields();
    }
  }
}
</script>

<style scoped>
.el-header, .el-footer {
  background-color: #B3C0D1;
  color: #333;
  text-align: center;
  line-height: 60px;
}

.el-aside {
  background-color: #D3DCE6;
  color: #333;
  text-align: center;
  line-height: 200px;
}

.el-main {
  /*background-color: #E9EEF3;*/
  color: #333;
  text-align: center;
  line-height: 160px;
}

body > .el-container {
  margin-bottom: 40px;
}

.el-container:nth-child(5) .el-aside,
.el-container:nth-child(6) .el-aside {
  line-height: 260px;
}

.el-container:nth-child(7) .el-aside {
  line-height: 320px;
}

.mlogo {
  height: 80%;
  border-radius: 50%;
  margin-top: 7px;
}

.demo-ruleForm {
  max-width: 500px;
  margin: 0 auto;
}
</style>
